package ngames.com.sushitv.temp.Module;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.temp.Class.AnimeSeries;
import ngames.com.sushitv.temp.Module.Interface.AnimeSeriesInterface;
import ngames.com.sushitv.temp.Util.HttpUtil_featured;
import ngames.com.sushitv.temp.model.RankData;
import ngames.com.sushitv.temp.model.SushiRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by edmond on 16-7-29.
 */
public class AnimeSeriesFactory implements AnimeSeriesInterface {
    private List<String> bannerUrls = new ArrayList<>();
    private List<AnimeSeries> animeSeriesArray = new ArrayList<>();
    @Override
    public void initData(final Handler handler) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1000);
                    bannerUrls.add("http://www.pptbz.com/pptpic/UploadFiles_6909/201204/2012041411433867.jpg");
                    bannerUrls.add("http://h.hiphotos.baidu.com/image/pic/item/8718367adab44aed098e6de0b11c8701a08bfbf6.jpg");
                    bannerUrls.add("http://c.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8e47e6d85c913495409237612.jpg");
                    bannerUrls.add("http://f.hiphotos.baidu.com/image/pic/item/9358d109b3de9c82c4f95c8f6e81800a19d84315.jpg");
                    bannerUrls.add("http://a.hiphotos.baidu.com/image/pic/item/1f178a82b9014a90f69971ecab773912b31bee3f.jpg");
                    Log.d("FeaturedFractory", "Here");
                    handler.sendEmptyMessage(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    @Override
    public void refreshData(final Handler handler) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1000);
                    bannerUrls = new ArrayList<String>();
                    bannerUrls.add("http://h.hiphotos.baidu.com/image/pic/item/3801213fb80e7bec43f0523e2b2eb9389a506bc2.jpg");
                    bannerUrls.add("http://pic3.nipic.com/20090622/2605630_113023052_2.jpg");
                    bannerUrls.add("http://b.hiphotos.baidu.com/image/pic/item/908fa0ec08fa513d17b6a2ea386d55fbb2fbd9e2.jpg");
                    bannerUrls.add("http://g.hiphotos.baidu.com/image/pic/item/2cf5e0fe9925bc313ab004755adf8db1ca137069.jpg");
                    Log.d("FeaturedFractory", "Here");
                    handler.sendEmptyMessage(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    public void initDataTop(Context context, String type,final OnLoadAnimeSeriesDataListListener listener) {
        getSimpleVideoData(context, type, listener);
    }

    @Override
    public void initDataNewOngoingAnime(Context context, String type,final OnLoadAnimeSeriesDataListListener listener) {
        getSimpleVideoData(context,type,listener);
    }

    @Override
    public void refreshTop(Context context, String type,final OnLoadAnimeSeriesDataListListener listener) {
        getSimpleVideoData(context,type,listener);
    }

    @Override
    public void refreshNewOngoingAnime(Context context, String type,final OnLoadAnimeSeriesDataListListener listener) {
        getSimpleVideoData(context,type,listener);
    }

    @Override
    public void getImage(ImageView image, String cover, Context context) {
        setImage(image,cover,context);
    }

    public List<AnimeSeries> getAnimeSeriesArray() {
        return animeSeriesArray;
    }

    public List<String> getBannerUrls() {
        return bannerUrls;
    }


    /**
     * 数据获取和解析
     * */
    public void getSimpleVideoData(Context context, final String type,final OnLoadAnimeSeriesDataListListener listener) {
        RequestQueue mQueue = Volley.newRequestQueue(context);
        int now = (int) (System.currentTimeMillis() / 1000);
        String url=null;
        if(type.equals("H_D")) {
            url = HttpUtil_featured.server_H_D  + AppApplication.getMyIMEI() + "&timestamp=" + now + HttpUtil_featured.aboutDevice;
        }else if(type.equals("H_0")){
            url = HttpUtil_featured.server_H_0  + AppApplication.getMyIMEI() + "&timestamp=" + now + HttpUtil_featured.aboutDevice;
        }else if(type.equals("H_1")){
            url = HttpUtil_featured.server_H_1  + AppApplication.getMyIMEI() + "&timestamp=" + now + HttpUtil_featured.aboutDevice;
        }
        Log.e("url",url);
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    String str = analyticalRequest(response);
                    if(type.equals("H_D")) {
                        List<RankData> rankDataList = JSON.parseArray(str.toString(), RankData.class);
                        listener.onSuccess(rankDataList, type);

                    }else{
                        JSONObject js = new JSONObject(str);
                        JSONArray ja = js.optJSONArray(type);
                        List<RankData> rankDataList = JSON.parseArray(ja.toString(), RankData.class);
                        listener.onSuccess(rankDataList, type);
                    }/*else{
                        JSONObject js = new JSONObject(str);
                        JSONArray ja = js.optJSONArray(type);
                        List<RankData> rankDataList = JSON.parseArray(ja.toString(), RankData.class);
                        listener.onSuccess(rankDataList, type);
                    }*/

                } catch (JSONException e) {
                    listener.onFailure("error",e);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //MyApplication.setTempStr("");
            }
        });
        mQueue.add(stringRequest);
    }
    public static String analyticalRequest(String s) {
        SushiRequest request = JSON.parseObject(s, SushiRequest.class);
        if (request.getCode().equals("0")) {
            try {
                return request.getData();
            } catch (Exception e) {

                Log.e("jsonString", e.toString());
                return null;
            }

        } else {
            return null;
        }
    }
    /**
     * 获取图片
     * */
    public static void setImage(ImageView view, String url, Context context) {
        RequestQueue mQueue = Volley.newRequestQueue(context);
        ImageLoader imageLoader = new ImageLoader(mQueue, new ImageLoader.ImageCache() {
            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            }

            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }
        });
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(view, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        imageLoader.get(url, listener);
    }
    public interface OnLoadAnimeSeriesDataListListener {
        void onSuccess(List<RankData> list, String flag);
        void onFailure(String msg, Exception e);
    }

}
